<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
class Model extends Common
{
    public function index()
    {
        $modelRes=db('model')->order('id desc')->paginate(5);
        $prefix=config("database.prefix");
        $this->assign(array(
            'modelRes'=>$modelRes,
            'prefix'=>$prefix
        ));
        return view();
    }
    public function add(){
        if(request()->ispost()){
            $data=input('post.');
            $validate=validate('model');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $add=db('model')->insert($data);
            if ($add){
                $tableName=$data['table_name'];
                $tableName=config("database.prefix").$tableName;
                $sql="create table {$tableName} (aid int unsigned not null) engine=MYISAM default charset=utf8";
                Db::execute($sql);
                $this->success('添加模型成功!',url('Model/index'));
            }else{
                $this->error('添加模型失败!');
            }
            return;
        }
        return view();
    }
    //ajax修改模型状态
    public function changeStatus(){
        if(request()->isAjax()){
            $modelid=input('modelid');
            $status=db('model')->field('status')->where('id',$modelid)->find();
            $status=$status['status'];
            if ($status==1){
                db('model')->where('id',$modelid)->update(['status'=>0]);
                echo 1;//由显示变隐藏
            }else{
                db('model')->where('id',$modelid)->update(['status'=>1]);
                echo 2;//由隐藏变显示
            }
        }else{
            $this->error('非法操作!');
        }

    }
    public function edit(){
        if (request()->isPost()){
            $data=input('post.');
            $oldTableName=db('model')->field('table_name')->find($data['id']);
            $oldTableName=$oldTableName['table_name'];
            $validate=validate('model');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
            $save=db('model')->update($data);
            if ( $oldTableName!=$data['table_name']){
                $prefix=config("database.prefix");
                $oldTableName=$prefix.$oldTableName;
                $tableName=$prefix.$data['table_name'];
                $sql="alter table {$oldTableName} rename {$tableName}";
                Db::execute($sql);
            }
            if ($save !==false){
                $this->success('修改模型成功!',url('Model/index'));
            }else{
                $this->error('修改模型失败!');
            }
            return;
        }
        $models=db('model')->find(input('id'));
        $this->assign('models',$models);
        return view();
    }
    //ajax删除模型
    public function ajaxDel(){

        $modelId=input('id');
        $tableName=input('table_name');
        $del=db('model')->delete($modelId);
        $sql="drop table {$tableName}";
        Db::execute($sql);
        if ($del){
            echo 1;
        }else{
            echo 0;
        }


    }
}
